
    function updateChatData(){
        //console.log('updating chat from id: '+_lastId);
        var e=document.getElementById(scriptId); 
        if (e!=undefined) {e.parentNode.removeChild(e);};
        scriptId="updChat"+Math.random().toString().replace(/\./g,"");
        var url=_baseAjaxPath+"getChatMessages/"+_lastId+"?"+scriptId;
        var headID=document.getElementsByTagName("head")[0]; var script=document.createElement('script');
        script.id=scriptId; script.type="text/javascript"; script.src=url; headID.appendChild(script);
    };
    function sendMessage(){
        nick=$('#nick').attr('value');
        if (nick=='') nick=$('#userLogin').attr('value');
        msg=$('#chatMsg').attr('value').replace(';','%3B').replace('&', '%26');
        if (nick!=undefined && msg!='') {
            $('#chatMsg').attr('value','');
            console.log('sending message from user '+nick);
            var e=document.getElementById(sendScriptId); 
            if (e!=undefined) {e.parentNode.removeChild(e);};
            sendScriptId="send"+Math.random().toString().replace(/\./g,"");
            var url=_baseAjaxPath+"send/?nick="+nick+"&text="+msg+"&"+sendScriptId;
            console.log('url: '+url);
            var headID=document.getElementsByTagName("head")[0]; var script=document.createElement('script');
            script.id=scriptId; script.type="text/javascript"; script.src=url; headID.appendChild(script);
            updateChatData();
        }
        return false;
    };
    function updateChatBox(){
        if (oldData!=data) {
            newText='';
            for(num=0;num<data.length;num+=1){
                one=data[num];
                if (one) {
                    timestamp=one['datetime'];
                    text = replaceURLWithHTMLLinks(one['text'])
                    string=timestamp+' <font color="#090"><b>'+one['nick']+'</b></font>&gt; <font color="#005">'+text+'</font><br>';
                    newText+=string;
                };
            };
            if (newText!=''){
                $('#chatbox').append(newText);
                var h=document.getElementById('chatbox').scrollHeight;
                $('#chatbox').animate({scrollTop: h+'px'}, 500);
            };
            oldData=data;
        };
    };
    function notify_sound() {
        
    }
    function replaceURLWithHTMLLinks(text) {
        var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
        return text.replace(exp,"<a href='$1'>$1</a>"); 
    }
    
    function loaderShow(){};
    function loaderHide(){};
    function sendMessage2(){
        nick=$('#nick').attr('value');
        if (nick=='') nick=$('#userLogin').attr('value');
        msg=$('#chatMsg').attr('value');
        console.log('sending message from user '+nick);
        if (msg!='' && nick!=''){
	        $.ajax({
		        async: false,
		        timeout: 4000,
		        url: _baseAjaxPath+"sendChatMessage/"+nick+"/"+msg,
		        global: false,
		        type: "GET",
		        beforeSend: function(){loaderShow();},
		        complete: function(){loaderHide();},
		        data: ({sendChatMessage: 1, nick: nick, message: msg}),
		        dataType: "html",
		        success: function(data){
		            console.log('message sent!');
                    update=window.setTimeout(updateChat, 500);
	            }
	        });
	        $('#chatMsg').attr('value','');
	    };
        return false;
    };
    _lastId=0;
    _baseAjaxPath='http://ekis2.ikito.ru:8080/ajax/';
    scriptId='none';
    sendScriptId='none';
    data='';
    oldData='';
    updateChatData();
    chatBoxUpdater=window.setInterval(updateChatBox,1000);
    chatDataUpdater=window.setInterval(updateChatData,3000);
    $('#chatForm').submit(sendMessage);

